package com.creditcmsplt.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil {

	private static Logger log = LoggerFactory.getLogger(FileUtil.class);
	
	public static void deleteFile(String filePath) {
		File file = new File(filePath);
		file.delete();
	}

	public static void deleteFiles(String... filePaths) {
		for (String string : filePaths) {
			File file = new File(string);
			file.delete();
		}
	}

	public static void copyFile(String source, String copy) {
		try {
			source = source.replace("\\", "/");
			copy = copy.replace("\\", "/");

			File source_file = new File(source);
			File copy_file = new File(copy);

			// BufferedStream缓冲字节流

			File parent = copy_file.getParentFile();
			// 创建复制路径
			if (!parent.exists()) {
				parent.mkdirs();
			}
			// 创建复制文件
			if (!copy_file.exists()) {
				copy_file.createNewFile();
			}

			FileInputStream fis = new FileInputStream(source_file);
			FileOutputStream fos = new FileOutputStream(copy_file);

			BufferedInputStream bis = new BufferedInputStream(fis);
			BufferedOutputStream bos = new BufferedOutputStream(fos);

			byte[] KB = new byte[1024];
			int index;
			while ((index = bis.read(KB)) != -1) {
				bos.write(KB, 0, index);
			}
			bos.close();
			bis.close();
			fos.close();
			fis.close();
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/**
	 * 写入字符串到文件
	 * 
	 * @param path
	 * @param con
	 */
	public static void writeStrToFile(String path, String con) {
		FileWriter writer;
		try {
			writer = new FileWriter(path);
			writer.write(con);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			log.error("", e);
		}
	}
	
	public static String readFileToStr(String path) {
		StringBuffer buffer=new StringBuffer();
		FileReader reader;
		try {
			reader = new FileReader(path);
			BufferedReader br = new BufferedReader (reader);
			String s;
			while ((s = br.readLine() )!=null) {
				buffer.append(s);
             }
			reader.close();
		} catch (IOException e) {
			log.error("", e);
		}
		return buffer.toString();
	}
	
	/**
	 * 校验文件大小
	 *
	 * @param
	 * @return
	 */
	public static boolean checkSize(byte[] bytes,double imgSize) {
	    //符合条件的照片大小（可配置） 单位：M
	    //double imgSize = 2.0;
	    //图片转base64字符串一般会大，这个变量就是设置偏移量。可配置在文件中，随时修改。目前配的是0。后续看情况适当做修改
	    double deviation = 0.0;
	    int length = bytes.length;
	    //原照片大小
	    double size = (double) length / 1024 / 1024 * (1 - deviation);
	    return size <= imgSize;
	}
	
	/**
	 * 判断base64是否为pdf文件
	 * @param bytes
	 * @return true ：是 false：否
	 */
	public static boolean checkPdf(byte[] bytes) {
		boolean resultFlag=true;
		try {
			PDDocument doc = PDDocument.load(bytes);
			doc.close();
		} catch (Exception e) {
			log.info("checkPdf：不是PDF文件");
			resultFlag=false;
		}
		return resultFlag;
	}
	
	/**
	 * 判断base64是否为pdf文件
	 * @param bytes
	 * @return true ：是 false：否
	 */
	public static boolean checkPdfBase64(String base64Str) {
		return checkPdf(Base64Util.bs64stringToBytes(base64Str));
	}
	
	/**
	 * 判断base64是否为pdf文件
	 * @param bytes
	 * @return true ：是 false：否
	 */
	public static boolean checkPdf(String filePath) {
		boolean resultFlag=true;
		try {
			File file = new File(filePath);
			PDDocument doc = PDDocument.load(file);
			doc.close();
		} catch (Exception e) {
			log.info("checkPdf：不是PDF文件");
			resultFlag=false;
		}
		return resultFlag;
	}
	
	public static void main(String[] args) {
		
		String abc=readFileToStr("D:/12365.txt");
		Base64Util.bs64stringToFile(abc, "G:/file/123.pdf");
		System.out.println(checkPdf("G:/file/123.pdf"));
	}

}
